package com.xposed.aliproxy.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xposed.aliproxy.utils.DateUtil;
import com.xposed.aliproxy.utils.ParamConfig;

import java.util.ArrayList;

public class DBUtil {
    private static DBUtil mInstance;
    private static SQLiteDatabase sqLiteDatabase;
    private final String f1715a = "tab_qrdata";
    private final String f1716b = "tab_alitrade";
    private Context context;
    private ParamConfig paramConfig;

    public static DBUtil getInstance(Context context) {
        if (mInstance == null || sqLiteDatabase == null) {
            mInstance = new DBUtil(context);
        }
        return mInstance;
    }

    public DBUtil(Context context) {
        sqLiteDatabase = new DBOpenHelper(context).getWritableDatabase();
        this.context = context;
        this.paramConfig = ParamConfig.getInstance(context);
    }

    public long insertAlitrade(AliTradeModel aliTradeModel) {
        String b = this.paramConfig.getString("agentId", "");
        String b2 = this.paramConfig.getString("aliAccount", "");
        String date = aliTradeModel.getDate();
        String a = DateUtil.formatDataByCustom(date.substring(0, 10), "yyyy-MM-dd", "yyyyMMdd");
        ContentValues contentValues = new ContentValues();
        contentValues.put("agentId", b);
        contentValues.put("aliAccount", b2);
        contentValues.put("localDate", a);
        contentValues.put("amount", aliTradeModel.getAmount());
        contentValues.put("date", date);
        contentValues.put("remark", aliTradeModel.getRemark());
        contentValues.put("outAliId", aliTradeModel.getOutAliId());
        contentValues.put("tradeNo", aliTradeModel.getTradeNo());
        contentValues.put("name", aliTradeModel.getName());
        contentValues.put("aliBalance", aliTradeModel.getAliBalance());
        contentValues.put("status", aliTradeModel.getStatus());
        contentValues.put("type", aliTradeModel.getType());
        return sqLiteDatabase.insert("tab_alitrade", null, contentValues);
    }

    public ArrayList<String> queryAlitrade() {
        String b = ParamConfig.getInstance(this.context).getString("agentId", "");
        String b2 = ParamConfig.getInstance(this.context).getString("aliAccount", "");
        Cursor query = sqLiteDatabase.query("tab_alitrade", new String[]{"localDate"}, "agentId=? and aliAccount=?", new String[]{b, b2}, "localDate", null, "localDate desc");
        ArrayList<String> arrayList = new ArrayList();
        while (query.moveToNext()) {
            arrayList.add(query.getString(query.getColumnIndex("localDate")));
        }
        query.close();
        return arrayList;
    }

    public ArrayList<AliTradeModel> queryAlitrade2(String str) {
        String b = ParamConfig.getInstance(this.context).getString("agentId", "");
        String b2 = ParamConfig.getInstance(this.context).getString("aliAccount", "");
        Cursor query = sqLiteDatabase.query("tab_alitrade", new String[]{"amount", "date", "remark", "name", "aliBalance", "tradeNo", "status"}, "localDate=? and agentId=? and aliAccount=?", new String[]{str, b, b2}, null, null, "id desc");
        ArrayList<AliTradeModel> arrayList = new ArrayList();
        while (query.moveToNext()) {
            AliTradeModel aliTradeModel = new AliTradeModel();
            aliTradeModel.setAmount(query.getString(query.getColumnIndex("amount")));
            aliTradeModel.setDate(query.getString(query.getColumnIndex("date")));
            aliTradeModel.setStatus(query.getString(query.getColumnIndex("status")));
            aliTradeModel.setRemark(query.getString(query.getColumnIndex("remark")));
            aliTradeModel.setName(query.getString(query.getColumnIndex("name")));
            aliTradeModel.setTradeNo(query.getString(query.getColumnIndex("tradeNo")));
            aliTradeModel.setAliBalance(query.getString(query.getColumnIndex("aliBalance")));
            arrayList.add(aliTradeModel);
        }
        query.close();
        return arrayList;
    }

    public void updateAlitrade(String str, String str2) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("status", str2);
        sqLiteDatabase.update("tab_alitrade", contentValues, "tradeNo=?", new String[]{str});
    }

    public boolean queryAlitradeByTradeNo(String str) {
        Cursor query = sqLiteDatabase.query("tab_alitrade", new String[]{"id"}, "tradeNo=?", new String[]{str}, null, null, null);
        if (query.moveToNext()) {
            query.close();
            return true;
        }
        query.close();
        return false;
    }

    public ArrayList<AliTradeModel> queryAlitradeByParams(String str, String str2) {
        Cursor query = sqLiteDatabase.query("tab_alitrade", new String[]{"name", "date", "amount", "tradeNo", "aliBalance", "remark"}, "status='0' and agentId=? and aliAccount=?", new String[]{str, str2}, null, null, null);
        ArrayList<AliTradeModel> arrayList = new ArrayList();
        while (query.moveToNext()) {
            AliTradeModel aliTradeModel = new AliTradeModel();
            aliTradeModel.setAmount(query.getString(query.getColumnIndex("amount")));
            aliTradeModel.setDate(query.getString(query.getColumnIndex("date")));
            aliTradeModel.setRemark(query.getString(query.getColumnIndex("remark")));
            aliTradeModel.setName(query.getString(query.getColumnIndex("name")));
            aliTradeModel.setTradeNo(query.getString(query.getColumnIndex("tradeNo")));
            aliTradeModel.setAliBalance(query.getString(query.getColumnIndex("aliBalance")));
            arrayList.add(aliTradeModel);
        }
        query.close();
        return arrayList;
    }
}
